0
1
00:00:00,920 --> 00:00:06,360
Now that we've covered the function basics, we can take it up a notch in complexity. In this lesson,
1

2
00:00:06,420 --> 00:00:13,080
we're gonna talk about functions with inputs, namely parameters and arguments.
2

3
00:00:13,220 --> 00:00:17,870
So I realized that if you're new to programming you're going to be hearing a lot of new words in these
3

4
00:00:17,870 --> 00:00:18,820
lessons.
4

5
00:00:18,920 --> 00:00:21,980
And one thing I'll say is that when you're learning anything new,
5

6
00:00:21,980 --> 00:00:25,000
the hard thing isn't necessarily the concepts,
6

7
00:00:25,010 --> 00:00:33,080
it's often the language. And it doesn't really matter if it's programming or medicine or cooking;
7

8
00:00:33,080 --> 00:00:36,890
every field has its own jargon and its own words.
8

9
00:00:36,890 --> 00:00:42,260
A big part of learning Python programming is learning some of this vocabulary.
9

10
00:00:42,350 --> 00:00:47,850
I'm going to be doing my best to introduce this vocabulary slowly over the course of the lessons. Now
10

11
00:00:47,840 --> 00:00:51,390
I realize that learning vocabulary words doesn't sound very fun.
11

12
00:00:51,590 --> 00:00:56,380
But the upside is that you're not gonna be learning words that people invented just to sound smart.
12

13
00:00:56,600 --> 00:01:04,010
Come to think of it, the Olympic champions of inventing meaningless words and catchphrases are probably
13

14
00:01:04,010 --> 00:01:06,380
the business consultants of this world.
14

15
00:01:06,380 --> 00:01:11,530
I've heard people say like the most ridiculous stuff with a completely straight face.
15

16
00:01:11,720 --> 00:01:18,050
I think my all time favorites are words like 'helicoptering' - which apparently means looking at things
16

17
00:01:18,050 --> 00:01:26,810
from above, and another one 'peeling the onion' -
which apparently means doing more analysis.
17

18
00:01:26,810 --> 00:01:32,090
So let's get back to Python programming, otherwise I won't be able to keep my lunch down.
18

19
00:01:32,090 --> 00:01:37,190
Looking at the code we've written previously we can see that some functions take inputs.
19

20
00:01:37,760 --> 00:01:46,530
So for example, this use function here had an input called 'fivethirtyeight' and our read_csv function had an input that
20

21
00:01:46,530 --> 00:01:55,310
was a file name. All of these inputs to functions go in between the parentheses after the function name.
21

22
00:01:55,390 --> 00:02:03,070
In contrast, our get_milk function had no inputs and this is the same with the show function, which also had
22

23
00:02:03,070 --> 00:02:04,840
no inputs.
23

24
00:02:04,840 --> 00:02:11,860
In contrast, the read_csv function had a piece of text between the parentheses and this piece of text
24

25
00:02:11,980 --> 00:02:19,300
was the input to the function. These inputs are called arguments or parameters.
25

26
00:02:19,300 --> 00:02:22,120
Let's create our own function that takes an input.
26

27
00:02:22,120 --> 00:02:26,220
That way we can put all of this into practice and see how the code actually works.
27

28
00:02:27,780 --> 00:02:34,880
I'm going to quickly copy paste this code from the get_milk function into my cell below and I'm gonna
28

29
00:02:34,890 --> 00:02:36,090
modify this code.
29

30
00:02:37,170 --> 00:02:39,550
First, I'm going to give this function a different name.
30

31
00:02:39,570 --> 00:02:48,070
I'm gonna call this function fill_the_fridge. Also, inside these parentheses,
31

32
00:02:48,320 --> 00:02:56,480
I'm going to specify an amount. This amount is gonna be our input to our function. Finally,
32

33
00:02:56,490 --> 00:02:59,450
I'm going to modify our third print statement down here.
33

34
00:02:59,450 --> 00:03:07,430
So where it says buy, I'm going to use the single quotes and the plus sign and then I'm going to put amount
34

35
00:03:07,430 --> 00:03:08,440
here.
35

36
00:03:08,930 --> 00:03:09,840
Like so.
36

37
00:03:09,860 --> 00:03:21,770
So it reads 'buy' + amount + ' cartons on the ground floor'. This way
37

38
00:03:21,850 --> 00:03:31,070
I'm making use of the input within the body of our function. The plus sign in this print statement concatenates
38

39
00:03:31,250 --> 00:03:36,550
our strings and by concatenate I mean merge or combine.
39

40
00:03:36,560 --> 00:03:39,140
Let's call this function to see it in action.
40

41
00:03:39,140 --> 00:03:47,060
So I'm going to hit Shift+Enter, and then I'm going to write fill_the_fridge, which is the name of the function
41

42
00:03:47,570 --> 00:03:49,390
followed by the parentheses.
42

43
00:03:49,640 --> 00:03:57,060
And then within the parentheses I'm going to put single quotes and write "five", and hit Shift+Enter.
43

44
00:03:57,260 --> 00:04:04,660
Now the piece of text that we entered between the parentheses shows up in our print statement.
44

45
00:04:04,700 --> 00:04:07,120
Let's think about what just happened here.
45

46
00:04:07,250 --> 00:04:14,150
First, you'll notice that the plus sign in Python actually means different things in different contexts.
46

47
00:04:15,270 --> 00:04:18,190
When the plus sign is used with numbers it adds them together.
47

48
00:04:18,270 --> 00:04:22,930
But when the plus sign is used with text it combines the strings.
48

49
00:04:23,100 --> 00:04:27,410
Let's call this function again but this time let's substitute a different amount.
49

50
00:04:27,480 --> 00:04:29,160
I'm going to write
50

51
00:04:29,640 --> 00:04:40,660
fill_the_fridge('one thousand') and hit Shift+Enter. Let's look at this code and think very carefully
51

52
00:04:40,660 --> 00:04:41,710
about what's going on.
52

53
00:04:42,740 --> 00:04:50,290
Our fill_the_fridge function takes one input, that input has a name and is called amount.
53

54
00:04:50,330 --> 00:04:55,700
But the really cool part is how amount is just a place holder.
54

55
00:04:55,700 --> 00:05:02,990
The value of amount is determined by whatever we specify when we call our fill_the_fridge function.
55

56
00:05:03,970 --> 00:05:11,070
The first time we specified the value "five" and amount held on to that piece of text.
56

57
00:05:11,320 --> 00:05:16,650
So the word "five" was printed in the print statement when we executed the function.
57

58
00:05:16,840 --> 00:05:23,590
But the second time we substituted the word "one thousand", so that the amount took on this value instead.
58

59
00:05:23,710 --> 00:05:28,270
And we saw "one thousand" printed out on the third line.
59

60
00:05:28,390 --> 00:05:35,670
In other words, amount behaves just like a variable - it gets its value when we call the function.
60

61
00:05:35,870 --> 00:05:43,820
And every time we refer to amount in the body of the function, we are able to use the value that's stored
61

62
00:05:43,820 --> 00:05:50,170
inside. Now there's two other things that you might notice about the code we just wrote.
62

63
00:05:50,270 --> 00:05:54,470
First, let's try to call this function with empty parentheses.
63

64
00:05:54,470 --> 00:06:02,290
So I'm going to write fill_the_fridge separated by underscores, and then have empty parentheses and hit
64

65
00:06:02,290 --> 00:06:12,330
Shift+Enter. In this case we get an error and the details of this error message read "Missing one required
65

66
00:06:12,510 --> 00:06:15,740
argument: amount".
66

67
00:06:15,790 --> 00:06:25,390
In other words, because we've defined our function with an input we must provide it. We specified one
67

68
00:06:25,390 --> 00:06:30,580
parameter in the function header so we must give a value.
68

69
00:06:30,580 --> 00:06:32,630
We can't just leave it out.
69

70
00:06:32,740 --> 00:06:39,190
The other thing you'll notice at this point is that print is nothing other than a function as well.
70

71
00:06:39,190 --> 00:06:49,490
Print takes a piece of text as an input and prints it out below the cell. So if print is a function, that
71

72
00:06:49,490 --> 00:06:59,350
means we're calling a function inside our function - we're nesting our function calls. Functions inside
72

73
00:06:59,350 --> 00:07:04,870
functions is like, I don't know, dreams within dreams in the movie Inception.
73

74
00:07:04,870 --> 00:07:09,330
In other words, one piece of code can trigger other bits of code.
74

75
00:07:09,820 --> 00:07:13,830
And this idea is actually really, really powerful when you think about it.
75

76
00:07:14,180 --> 00:07:16,010
So here's a quick recap.
76

77
00:07:16,130 --> 00:07:23,840
When working with functions with inputs, we specify what the input is upfront when defining the function.
77

78
00:07:24,500 --> 00:07:33,360
The input is specified between the parentheses following the function name. In this example, the input
78

79
00:07:33,540 --> 00:07:41,490
is called money and money is just a place holder. In programming jargon when referring to an input as
79

80
00:07:41,490 --> 00:07:44,640
a place holder or as a variable,
80

81
00:07:44,640 --> 00:07:53,200
we tend to call it a parameter as in: get_milk has one parameter called money.
81

82
00:07:53,420 --> 00:07:57,630
Now, remember how I said we're going to have some vocabulary training?
82

83
00:07:57,630 --> 00:08:03,520
Well, when we call the function we must provide a value to this parameter.
83

84
00:08:03,750 --> 00:08:11,010
In this case we must provide a number - that value is gonna be 20.5 when I call this function.
84

85
00:08:11,880 --> 00:08:15,370
The value that's passed to a function when it's being called
85

86
00:08:15,420 --> 00:08:17,720
is called an argument.
86

87
00:08:17,960 --> 00:08:23,580
Now oftentimes you'll see the words parameter and argument used interchangeably.
87

88
00:08:23,580 --> 00:08:29,970
People aren't always super pedantic in the real world, but if you want to express yourself precisely
88

89
00:08:30,300 --> 00:08:36,780
or get technical it's good to know these definitions. A parameter is when we're referring to the inputs
89

90
00:08:36,780 --> 00:08:42,870
to a function and we're talking about it as the place holder or as the variable and we use the word
90

91
00:08:42,960 --> 00:08:48,030
argument when we're talking about the actual value that we're giving the function when we're calling
91

92
00:08:48,030 --> 00:08:49,870
it. But,
92

93
00:08:49,880 --> 00:08:55,700
that's probably enough vocabulary training for now. And we've also covered quite a few different kinds
93

94
00:08:55,700 --> 00:08:57,590
of functions so far.
94

95
00:08:57,590 --> 00:09:06,590
For example, we understand the syntax behind show and we also understand the syntax behind use or read_csv.
95

96
00:09:06,590 --> 00:09:14,830
Show had no arguments, but read_csv has a file name as an argument.
96

97
00:09:14,930 --> 00:09:18,580
Now we're gonna look at functions with multiple arguments.
97

98
00:09:19,070 --> 00:09:26,010
We've actually encountered multiple arguments when we called the scatter function for our plot and we
98

99
00:09:26,010 --> 00:09:33,910
also had multiple arguments when we called the fit function for our regression - fit had two arguments
99

100
00:09:33,910 --> 00:09:41,770
capital X and lowercase y. X was our movie budgets and lowercase y was our movie revenue.
100

101
00:09:41,770 --> 00:09:48,290
We can see that these two arguments are separated by a comma, but let's create our own function and have
101

102
00:09:48,290 --> 00:09:54,930
it take more than one argument so that we can see this in practice. Back in the Python intro notebook,
102

103
00:09:54,980 --> 00:10:01,860
I'm going to copy our previous function that we defined here - the fill_the_fridge function and I'm going
103

104
00:10:01,860 --> 00:10:06,290
to paste that into the cell where we had the error.
104

105
00:10:06,380 --> 00:10:11,880
I'm now going to modify this function; first by changing the name.
105

106
00:10:11,950 --> 00:10:19,870
So I'm going to call this function milk_mission and then I'm also going to change the function
106

107
00:10:19,870 --> 00:10:20,770
header.
107

108
00:10:20,770 --> 00:10:25,770
I'm going to change the number of parameters that are between these two parentheses.
108

109
00:10:25,960 --> 00:10:30,140
So after amount I'm going to add another parameter.
109

110
00:10:30,370 --> 00:10:34,460
I'm going to add a destination.
110

111
00:10:35,110 --> 00:10:38,600
We're going to use this parameter in our second print statement.
111

112
00:10:39,680 --> 00:10:43,390
Which, instead of reading "walk to the store" we're going to have it read
112

113
00:10:43,490 --> 00:10:48,350
'walk to the' + destination
113

114
00:10:48,640 --> 00:10:57,400
So we're gonna concatenate the string walk to the with whatever is supplied as a destination.
114

115
00:10:57,540 --> 00:10:59,860
I can't wait to try out our new function.
115

116
00:10:59,860 --> 00:11:05,030
So I'm going to hit Shift+Enter and then in the cell below I'm going to call this function.
116

117
00:11:05,030 --> 00:11:07,470
I'm going to say milk_mission
117

118
00:11:10,610 --> 00:11:17,970
and for amount I'm going to write 'twenty' and for destination I'm going to write in single quotes
118

119
00:11:20,620 --> 00:11:22,320
'department store'.
119

120
00:11:22,360 --> 00:11:29,380
Here we can see that our first argument 'twenty' is gonna be for the amount and our second argument 'department
120

121
00:11:29,380 --> 00:11:33,920
store' is gonna be for destination. When I hit Shift+Enter,
121

122
00:11:33,950 --> 00:11:40,040
I can see these values substituted into our print statement accordingly.
122

123
00:11:40,050 --> 00:11:48,360
Now here's the question - what do you think will happen if instead of writing milk_mission('twenty', 'department
123

124
00:11:48,360 --> 00:11:55,630
store') we write milk_mission('department store', 'twenty')?
124

125
00:11:58,830 --> 00:12:06,850
You guessed it. I walk to the 'twenty' and I buy 'department store cartons of milk'. What this tells us is that
125

126
00:12:06,850 --> 00:12:11,300
our arguments to our function are passed by their position.
126

127
00:12:11,620 --> 00:12:18,250
In other words - the order in which we give the inputs to our function matters and it matters quite a
127

128
00:12:18,250 --> 00:12:19,270
bit.
128

129
00:12:19,270 --> 00:12:21,740
The arguments are passed from left to right.
129

130
00:12:21,910 --> 00:12:30,100
So, as before, we must supply exactly two arguments when recalling milk mission but we also must be careful
130

131
00:12:30,310 --> 00:12:34,570
to supply these arguments in the right order.
131

132
00:12:34,630 --> 00:12:40,690
Now, looking at the syntax for calling a function, it still strikes me kind of strange every time I think
132

133
00:12:40,690 --> 00:12:41,970
about it.
133

134
00:12:42,340 --> 00:12:45,010
Imagine how that would play out in real life,
134

135
00:12:45,010 --> 00:12:51,520
imagine your girlfriend or boyfriend telling you to go to the supermarket and saying go buy eggs, potatoes,
135

136
00:12:51,520 --> 00:12:56,930
milk cartons, doughnuts and apples by 2, 10, 1, 12 and 5.
136

137
00:12:56,950 --> 00:12:59,500
This is confusing, right?
137

138
00:12:59,680 --> 00:13:08,680
And yet many programming languages work exactly like this. But confusion is bad and nobody wants to come
138

139
00:13:08,680 --> 00:13:13,540
back from the supermarket and have an argument with their partner because they've messed up the order
139

140
00:13:13,540 --> 00:13:15,480
of their arguments.
140

141
00:13:15,490 --> 00:13:20,260
Lucky for us, Python can actually help us have a successful relationship.
141

142
00:13:20,260 --> 00:13:24,040
It provides an alternative for passing arguments by position.
142

143
00:13:24,280 --> 00:13:31,540
We can also match our arguments by providing a name. Now, believe it or not, we actually did this previously
143

144
00:13:31,690 --> 00:13:33,970
in our notebook.
144

145
00:13:33,970 --> 00:13:41,710
Check out how we called scatter for instance - we had X, y, color = 'blue', alpha = 
145

146
00:13:41,830 --> 00:13:43,830
0.3)
146

147
00:13:44,200 --> 00:13:51,340
The point that I want to draw your attention to is the part where it says color = 'blue' and alpha =
147

148
00:13:51,400 --> 00:13:53,190
0.3
148

149
00:13:53,200 --> 00:13:57,130
Here we are matching our arguments by name.
149

150
00:13:57,190 --> 00:14:03,560
Let's go back to our milk_mission function. Remember how when we got the order wrong, we printed nonsense?
150

151
00:14:04,520 --> 00:14:08,440
Well, we can add the name of our parameters to the function call
151

152
00:14:08,690 --> 00:14:12,530
so that Python will match these values by keyword.
152

153
00:14:12,530 --> 00:14:23,760
So if we say destination = 'department store' and amount = 'twenty' and press Shift+Enter,
153

154
00:14:24,080 --> 00:14:31,160
we can see that the output below the cell updates to the way that we wanted to, namely "Walk to the department
154

155
00:14:31,160 --> 00:14:33,350
store", "Buy twenty cartons on the ground floor".
155

156
00:14:34,220 --> 00:14:40,490
So here we are setting the destination equal to 'department store' and we're setting the amount equal to twenty.
156

157
00:14:41,920 --> 00:14:44,460
Looking at the code where we called scatter again,
157

158
00:14:44,500 --> 00:14:51,370
we can actually see that in this case we're kind of mixing and matching supplying arguments by position
158

159
00:14:51,790 --> 00:14:57,650
and by name. The equivalent when we're calling milk_mission would look something like this
159

160
00:15:01,610 --> 00:15:14,130
we'd have milk_mission('twenty', destination='store') and hit Shift+Enter.
160

161
00:15:14,150 --> 00:15:21,530
In this case the argument 'twenty' is set by position and the destination is set by name.
161

162
00:15:21,530 --> 00:15:25,440
Now, personally I actually quite like named arguments.
162

163
00:15:25,670 --> 00:15:29,330
They make the Python code much more readable.
163

164
00:15:29,330 --> 00:15:31,920
It's like having labels for the data.
164

165
00:15:31,940 --> 00:15:41,010
Suppose I went back where we called scatter and I deleted  the names for these arguments.
165

166
00:15:41,010 --> 00:15:44,940
In this case we just have X, y, 'blue', 0.3.
166

167
00:15:45,210 --> 00:15:52,050
Anybody looking at this code is gonna wonder what on earth this 0.3 and that somebody might
167

168
00:15:52,050 --> 00:15:55,440
as well be yourself in like two weeks time when you forgot what you did.
168

169
00:15:56,750 --> 00:16:02,330
I guess the message I'm trying to convey here is that having a bunch of naked values separated by commas
169

170
00:16:02,690 --> 00:16:08,750
doesn't make the code very readable to us humans. Providing these keywords and calling a function just
170

171
00:16:08,750 --> 00:16:11,220
makes everything so much more readable.
171

172
00:16:11,260 --> 00:16:18,800
So here's a question for you - what do you think will happen if we call our function like this 
172

173
00:16:18,810 --> 00:16:27,340
milk_mission('store', amount='twenty'). Hitting Shift+Enter,
173

174
00:16:27,340 --> 00:16:35,020
we see that we get an error, and the reason is is that we're trying to assign a value to amount twice -
174

175
00:16:35,680 --> 00:16:43,570
the first time we're assigning the 'store' value to amount because Python is evaluating our arguments
175

176
00:16:43,600 --> 00:16:48,890
by their position and the second time we're setting amount equal to 'twenty'
176

177
00:16:49,000 --> 00:16:52,640
by using the keyword. Okay, cool.
177

178
00:16:52,640 --> 00:16:59,830
So we're coming to the end of this lesson on parameters and arguments and we've covered a lot of material
178

179
00:16:59,830 --> 00:17:00,910
here.
179

180
00:17:01,000 --> 00:17:05,370
This material will sink in more with practice, I promise.
180

181
00:17:05,440 --> 00:17:11,410
And if you've heard any recent buzzwords that make you want to stick needles in your eyes please share
181

182
00:17:11,410 --> 00:17:12,550
them in this lesson's
182

183
00:17:12,550 --> 00:17:15,460
Q&A section. I'd love to hear them.
183

184
00:17:15,790 --> 00:17:17,140
See you in the next lesson.
